use warnings;
use strict;
use Test::More tests => 11;
use ReefStats::Schema::ReefStatsDB;

my $schema = ReefStats::Schema::ReefStatsDB->connect_for_testing;

isa_ok( $schema, 'ReefStats::Schema::ReefStatsDB' );

my $domains = $schema->resultset('Domains');
$domains->delete_all();
is( $domains->count(), 0, 'starting out with an empty domain set' );

my $nakedcode = $domains->create( { domain => 'www.nakedcode.com', } );

is( $nakedcode->domain, 'www.nakedcode.com', 'got the right domain back' );
like( $nakedcode->id, qr/^\d+$/, 'the id looks good' );
is( $domains->count(), 1, 'got a domain now' );

my $perlcast = $domains->create( { domain => 'www.perlcast.com', } );

is( $perlcast->domain, 'www.perlcast.com', 'got the right domain back' );
like( $perlcast->id, qr/^\d+$/, 'the id looks good' );
is( $domains->count(), 2, 'two domains' );

is( $domains->search( { domain => 'www.perlcast.com' } )->first->id,
    $perlcast->id, 'found the domain that we were looking for' );

my $users = $schema->resultset('Users');
$users->delete_all();
is( $users->count(), 0, 'starting out with an empty domain set' );

my $first_user = $users->create(
    {
        email_address => 'me@nakedcode.com',
        password      => 'hello',
        domain        => $nakedcode,
    }
);
is( $first_user->domain->domain, 'www.nakedcode.com' );

$schema->txn_rollback();
